package data.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class DownloadUrlList {
	DatabaseConnection databaseConnection;
	
	private String table;		//which table in database
	private String keyField;	//which field is unique key
	private String urlField;	//which field is url
	private String flagField;	//which field is the visited mark
	
	private int current = 0;
	public ArrayList<VideoRecord> list = new ArrayList<VideoRecord>();
	
	
	public DownloadUrlList(String table, String keyField, String urlField, String flagField){
		this.table = table;
		this.keyField = keyField;
		this.urlField = urlField;
		this.flagField = flagField;
					
		databaseConnection = new DatabaseConnection("127.0.0.1:3306/videose", "root", "ladjzero");
		String query = "SELECT * FROM "+table+" WHERE "+flagField+"=0";
		ResultSet rs = databaseConnection.executeQuery(query);
		try {
			while(rs.next()){
				list.add( new VideoRecord( rs.getString( keyField ), rs.getString( urlField ), rs.getString( "title" )));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//databaseConnection.destroy();
	}
	
	public String getCurrentUrl(){
		return list.get(current).url;
	}
	
	public void nextAndUpdateFlag(){
		String updateQuery = "UPDATE "+table+" SET "+flagField+"=1 WHERE "+keyField+"="+list.get(current).id;
		databaseConnection.updataOneRecord(updateQuery);
		current++;
	}
	
	public void next(){
		current++;
	}
	
	public boolean isEnd(){
		if(list.size() == current)
			return true;
		else
			return false;
	}
}
